package com.hyzx.qbasic.admin.controller.system;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.constant.PermissionConst;
import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.po.SysConfigPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.*;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.admin.domain.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * Created by 黄江华 on 2018/7/23
 */


@Api(tags = "系统配置", description = "SysConfigController")
@Controller
@RequestMapping("/sys/config")
public class SysConfigController extends BaseController {

    @Autowired
    private ISysConfigService configService;

    @ApiIgnore
    @SysLog("系统配置首页")
    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("addFunc", getFunction(PermissionConst.SYS_CONFIG_ADD));
        model.addAttribute("editFunc", getFunction(PermissionConst.SYS_CONFIG_EDIT));
        model.addAttribute("deleteFunc", getFunction(PermissionConst.SYS_CONFIG_DELETE));
        return "system/config/list";
    }

    @ApiOperation("获取系统配置列表")
    @GetMapping("/getConfigList")
    @ResponseBody
    public BaseResModel<PageInfo<SysConfigPO>> getConfigList(@Valid GetConfigListReq req, BindingResult result) {
        return BaseResUtils.success(configService.getPagedConfigList(req));
    }

    @ApiIgnore
    @SysLog("新增系统配置页")
    @GetMapping("/add")
    public String add() {
        return "system/config/add";
    }

    @ApiOperation("新增系统配置表单提交")
    @PostMapping("/add")
    @ResponseBody
    public BaseResModel<BaseOperationRes> add(@Valid @RequestBody AddConfigReq req, BindingResult result) {
        BaseOperationRes res = configService.addConfig(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("删除系统配置")
    @PostMapping("/delete")
    @ResponseBody
    public BaseResModel<BaseOperationRes> delete(@Valid @RequestBody DeleteConfigReq req, BindingResult result) {
        BaseOperationRes res = configService.deleteConfig(req);
        return BaseResUtils.success(res);
    }

    @ApiIgnore
    @SysLog("编辑系统配置页")
    @GetMapping("/edit")
    public String edit() {
        return "system/config/edit";
    }

    @ApiOperation("编辑系统配置表单提交")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> edit(@Valid @RequestBody EditConfigReq req, BindingResult result) {
        BaseOperationRes res = configService.editConfig(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("获取系统配置信息")
    @GetMapping("/getConfigInfo")
    @ResponseBody
    public BaseResModel<SysConfigPO> getConfigInfo(@Valid GetConfigInfoReq req, BindingResult result) {
        return BaseResUtils.success(configService.getConfigInfoById(req.getConfId()));
    }
}
